package com.goktech.olala.client.controller.order;

import com.goktech.olala.core.resp.RespBuyInfoVo;
import com.goktech.olala.core.resp.RespShopCartVo;
import com.goktech.olala.core.service.IShopcartService;
import com.goktech.olala.utils.RMBUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname OederCartController
 * @Description TODO
 * @Date 2020/12/26 15:19
 * @Created by sanming
 */
@RestController
@RequestMapping("/orderApi")
public class OederDetilController {

    @Autowired
    IShopcartService iShopcartService;

    @RequestMapping(value = "/settleShopCart.do")
    @ResponseBody
    public ModelAndView settleShopCart(HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView();
        String customerId = (String)request.getSession().getAttribute("customerId");
        if(StringUtils.isBlank(customerId)){
            mv.setViewName("home/login");
            return mv;
        }
        String shopCartIdArrStr = request.getParameter("shopCartIdArr");
        String amountArrStr = request.getParameter("amountArr");
        String[] shopCartIdArr = shopCartIdArrStr.split(",");
        String[] amountArr = amountArrStr.split(",");
        List<RespBuyInfoVo> respBuyInfoVoList = new ArrayList<>();
        for(int i=0;i<shopCartIdArr.length;i++){
            String shopCartId = shopCartIdArr[i];
            int amount = Integer.valueOf(amountArr[i]);
            RespBuyInfoVo respBuyInfoVo = new RespBuyInfoVo();
            RespShopCartVo respShopCartVo = iShopcartService.queryShopCartById(shopCartId);
            respBuyInfoVo.setGoodsId(respShopCartVo.getGoodsId());
            respBuyInfoVo.setGoodsName(respShopCartVo.getGoodsName());
            respBuyInfoVo.setGoodsAttr(respShopCartVo.getGoodsAttr());
            String realBuyPrice = respShopCartVo.getRealBuyPrice();
            int price = Integer.valueOf(RMBUtil.changeY2F(realBuyPrice));
            respBuyInfoVo.setOrderMoney(RMBUtil.changeF2Y(price*amount+""));
            respBuyInfoVo.setPayMoney(RMBUtil.changeF2Y(price*amount+""));
            respBuyInfoVo.setGoodsAmount(amount);
            respBuyInfoVo.setGoodsPrice(realBuyPrice);
            respBuyInfoVo.setCustomerId(customerId);

            respBuyInfoVoList.add(respBuyInfoVo);
        }

        mv.setViewName("home/pay");


        return mv;
    }
}
